package com.guguskins.back.controller;


import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuInviteNum;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.entity.vo.UserInviteVo;
import com.guguskins.back.service.GuUserInviteService;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户邀请详情表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/userInvite")
public class GuUserInviteController {

    @Autowired
    private GuUserInviteService userInviteService;

    /**
     * 推广中心
     */
    @GetMapping("/getList")
    public ResultVo getList(@RequestHeader(value = "token", required = false, defaultValue = "") String token) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        UserInviteVo userInviteVo = userInviteService.getList(userId);
        return ResultVo.success(userInviteVo);
    }

    @PostMapping("/receivePrize")
    @Authorized
    public ResultVo receivePrize(@RequestHeader("token") String token,
                                 @RequestBody GuInviteNum inviteNum) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        userInviteService.receivePrize(userId, inviteNum.getId());
        return ResultVo.success("领取成功");
    }

    @GetMapping("/back/list")
    public ResultVo findAll() {
        return ResultVo.success(userInviteService.list(), "查找成功");
    }


}
